﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class BindPickingPositionRepository : BaseRepository<BindPickingPositionModel>, IBindPickingPositionRepository
    {
        public BindPickingPositionRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 绑定拣选位
        /// </summary>
        /// <param name="position"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool AddBindPickingPosition(BindPickingPositionModel position,out string msg)
        {
            msg = "";
            var BindPickingList = Context.BindPickingPositionModel.Where(x => x.PickingPositionCode == position.PickingPositionCode).ToList();
            if(BindPickingList.Count() <= 0)
            {
                msg = "暂无拣选位 "+ position.PickingPositonName;
                return false;
            }
            var BindPickingListFirst = BindPickingList.First();
            if((BindPickingListFirst.NowLoginUserId != 0 || BindPickingListFirst.NowLoginUserName != null) && BindPickingListFirst.Status == 1)
            {
                msg = "拣选位" + position.PickingPositonName + "已被占用";
                return false;
            }
            BindPickingListFirst.NowLoginUserId = position.NowLoginUserId;
            BindPickingListFirst.NowLoginUserName = position.NowLoginUserName;
            BindPickingListFirst.Status = 1;

            // 更新现有实体的属性
            Context.BindPickingPositionModel.Update(BindPickingListFirst);
            if (Context.SaveChanges() <= 0)
            {
                msg = "更新失败";
                return false;
            }
            return true;
        }

        /// <summary>
        /// 解除拣选位
        /// </summary>
        /// <param name="PickingPosition"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelBindPickingPosition(BindPickingPositionModel position, out string msg)
        {
            msg = "";
            var BindPickingPositionList = Context.BindPickingPositionModel.Where(x => x.PickingPositionCode == position.PickingPositionCode).ToList();
            if(BindPickingPositionList.Count <= 0)
            {
                msg = "暂无拣选台" + position.PickingPositionCode;
                return false;
            }
            var BindPickingListFirst = BindPickingPositionList.First();
            if(BindPickingListFirst.Status == 0)
            {
                msg = "拣选台" + position.PickingPositionCode + "目前处于未绑定状态";
                return false;
            }
            if(BindPickingListFirst.NowLoginUserId != position.NowLoginUserId)
            {
                msg = "请登录绑定拣选人的账号进行解除";
                return false;
            }
            BindPickingListFirst.NowLoginUserId = 0;
            BindPickingListFirst.NowLoginUserName = "";
            BindPickingListFirst.Status = 0;
            Context.BindPickingPositionModel.Update(BindPickingListFirst);
            if (Context.SaveChanges() <= 0)
            {
                msg = "更新失败";
                return false;
            }
            return true;
        }

        /// <summary>
        /// 获取所有拣选位
        /// </summary>
        /// <returns></returns>
        public List<PickingPositionModel> GetBindPickingPosition()
        {
            var list = Context.PickingPositionModel.AsQueryable().ToList();
            return list;

        }
    }
}
